﻿Imports WebSitePanel.SkinDesigner.Business
Imports System.Web
Imports System.IO

Public Class ucImages
    Private _selecteditem As ucIcon
    Public Sub UpdateImages(ByVal Images As List(Of String))
        Me.flpImages.SuspendLayout()
        Me.flpImages.Controls.Clear()
        Dim list As New List(Of String)
        For Each item As String In Images
            item = HttpUtility.UrlDecode(item)
            If Not list.Contains(item) Then
                Dim pb As New ucIcon(item)
                pb.Tag = item
                AddHandler pb.MouseDown, AddressOf ucIcon_MouseDown
                AddHandler pb.MouseEnter, AddressOf ucIcon_MouseEnter
                AddHandler pb.MouseLeave, AddressOf ucIcon_MouseLeave
                Me.flpImages.Controls.Add(pb)
                list.Add(item)
            End If
        Next
        Me.btnDefault.Enabled = False
        Me.btnReplace.Enabled = False
        Me.ClearPreview()
        Me.flpImages.ResumeLayout()
    End Sub
    Private Sub ClearPreview()
        Me.tlpPreview.Visible = False
    End Sub
    Private Sub SetPreview()
        Dim file As String = _selecteditem.pb.ImageLocation
        pbFilePreview.ImageLocation = file
        Dim extension As String = Path.GetExtension(file)
        extension = extension.Substring(1)
        Me.lblFileType.Text = extension.ToUpper & " File"
        Me.lblFileName.Text = Path.GetFileName(file)
        Dim b As Bitmap = New Bitmap(_selecteditem.pb.ImageLocation)
        Me.lblDimensionsValue.Text = b.Width & " x " & b.Height
        b.Dispose()
        Dim fi As New FileInfo(file)
        Me.lblFileSizeValue.Text = CInt(fi.Length / 1000) & "KB"
        tlpPreview.Visible = True
    End Sub
    Private Sub ucIcon_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        CType(sender, UserControl).BorderStyle = BorderStyle.FixedSingle
        _selecteditem = sender
        Me.btnDefault.Enabled = True
        Me.btnReplace.Enabled = True
        For Each item As ucIcon In flpImages.Controls
            If item IsNot sender Then
                item.BorderStyle = BorderStyle.None
            End If
        Next
        SetPreview()
    End Sub
    Public Sub ucIcon_MouseEnter(ByVal Sender As Object, ByVal e As System.EventArgs)
        CType(Sender, UserControl).BorderStyle = BorderStyle.FixedSingle
    End Sub
    Public Sub ucIcon_MouseLeave(ByVal Sender As Object, ByVal e As System.EventArgs)
        If Sender IsNot _selecteditem Then
            CType(Sender, UserControl).BorderStyle = BorderStyle.None
        End If
    End Sub
    Public Sub Unload()
        For Each item As ucIcon In flpImages.Controls
            item.Unload()
        Next
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplace.Click
        Dim ext As String = IO.Path.GetExtension(CStr(_selecteditem.Tag))
        ofdIcon.Filter = ext & "-files|*" & ext
        If ofdIcon.ShowDialog = Windows.Forms.DialogResult.OK Then
            CommandManager.Instance.ExecuteCommand(New ReplaceImageCommand(_selecteditem.Tag, ofdIcon.FileName))
            _selecteditem.RefreshPicture()
        End If
    End Sub

    Private Sub btnDefault_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDefault.Click
        CommandManager.Instance.ExecuteCommand(New DefaultIconCommand(_selecteditem.Tag))
        _selecteditem.RefreshPicture()
    End Sub

    Private Sub btnDefault_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDefault.MouseEnter
        btnDefault.BackgroundImage = My.Resources.defaulthover
    End Sub

    Private Sub btnDefault_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDefault.MouseLeave
        btnDefault.BackgroundImage = My.Resources.defaultimg
    End Sub

    Private Sub btnReplace_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReplace.MouseEnter
        btnReplace.BackgroundImage = My.Resources.replacehover
    End Sub

    Private Sub btnReplace_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReplace.MouseLeave
        btnReplace.BackgroundImage = My.Resources.replace
    End Sub
End Class
